import REQ from "@/api/web/REQ.ts";
import {Res} from "@/api/objects";
import axios, {AxiosResponse} from "axios";

export interface UserDto {
    id: string
    username: string
    userType: string
    createTime: string
    updateTime: string
}

class UserApi {
    login = async (username: string, password: string) => {
        return await REQ<{ username: string, password: string }, Res<string>>({
            url: "/api/user/login",
            method: "post",
            data: {username, password},
        })
    }

    createUser = async (username: string, password: string) => {
        return await REQ<{ username: string, password: string }, Res<string>>({
            url: "/api/user/create",
            method: "post",
            data: {username, password},
        })
    }

    logout = async () => {
        return await REQ<null, Res<void>>({
            url: "/api/user/logout",
            method: "get",
        })
    }

    getSelf = async (): Promise<Res<UserDto>> => {
        return await axios.get<any, AxiosResponse<Res<UserDto>>, any>("/api/user/self")
            .then(response => response.data);
    }

    getUserById = async (creatorId: string) => {
        return await REQ<null, Res<UserDto>>({
            method: 'get',
            url: "/api/user/info/" + creatorId,
        })
    }
}

export default new UserApi()
